<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>卖家管理测试 - 阿里宝宝</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        button { padding: 10px 20px; margin: 5px; }
        .btn-primary { background-color: #007bff; color: white; border: none; }
        .btn-success { background-color: #28a745; color: white; border: none; }
        .btn-danger { background-color: #dc3545; color: white; border: none; }
    </style>
</head>
<body>
    <h1>卖家管理系统测试</h1>
    
    <div class="test-section">
        <h3>1. 卖家注册测试</h3>
        <button class="btn-primary" onclick="testSellerRegister()">注册卖家账户</button>
        <div id="registerResult"></div>
    </div>

    <div class="test-section">
        <h3>2. 卖家登录测试</h3>
        <button class="btn-primary" onclick="testSellerLogin()">卖家登录</button>
        <div id="loginResult"></div>
    </div>

    <div class="test-section">
        <h3>3. 获取店铺信息</h3>
        <button class="btn-primary" onclick="testGetShop()">获取店铺信息</button>
        <div id="shopResult"></div>
    </div>

    <div class="test-section">
        <h3>4. 添加商品测试</h3>
        <button class="btn-success" onclick="testAddProduct()">添加商品</button>
        <div id="addProductResult"></div>
    </div>

    <div class="test-section">
        <h3>5. 获取商品列表</h3>
        <button class="btn-primary" onclick="testGetProducts()">获取商品列表</button>
        <div id="productsResult"></div>
    </div>

    <div class="test-section">
        <h3>6. 更新店铺信息</h3>
        <button class="btn-primary" onclick="testUpdateShop()">更新店铺信息</button>
        <div id="updateShopResult"></div>
    </div>

    <div class="test-section">
        <h3>7. 获取统计数据</h3>
        <button class="btn-primary" onclick="testGetStats()">获取统计数据</button>
        <div id="statsResult"></div>
    </div>

    <script>
        let currentSeller = null;

        async function testSellerRegister() {
            const result = document.getElementById('registerResult');
            try {
                const response = await fetch('http://localhost:8000/api/auth/register', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        username: 'testseller' + Date.now(),
                        password: '123456',
                        email: 'testseller@example.com',
                        phone: '13800000000',
                        userType: 'Seller',
                        shopName: '测试店铺'
                    })
                });
                
                const data = await response.json();
                if (data.success) {
                    currentSeller = data.data;
                    result.innerHTML = `<div class="success">✓ 卖家注册成功: ${data.data.username}</div>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 注册失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 注册失败: ${error.message}</div>`;
            }
        }

        async function testSellerLogin() {
            const result = document.getElementById('loginResult');
            try {
                const response = await fetch('http://localhost:8000/api/auth/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        username: 'seller001',
                        password: '123456'
                    })
                });
                
                const data = await response.json();
                if (data.success) {
                    currentSeller = data.data;
                    result.innerHTML = `<div class="success">✓ 卖家登录成功: ${data.data.username}</div>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 登录失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 登录失败: ${error.message}</div>`;
            }
        }

        async function testGetShop() {
            const result = document.getElementById('shopResult');
            if (!currentSeller) {
                result.innerHTML = '<div class="error">请先登录</div>';
                return;
            }
            
            try {
                const response = await fetch(`http://localhost:8000/api/seller/shop?username=${currentSeller.username}`);
                const data = await response.json();
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 店铺信息: ${data.data.name}</div>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 获取店铺信息失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 获取店铺信息失败: ${error.message}</div>`;
            }
        }

        async function testAddProduct() {
            const result = document.getElementById('addProductResult');
            if (!currentSeller) {
                result.innerHTML = '<div class="error">请先登录</div>';
                return;
            }
            
            try {
                const response = await fetch('http://localhost:8000/api/seller/products', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        username: currentSeller.username,
                        name: '测试商品' + Date.now(),
                        price: 99.99,
                        category: 'Food',
                        location: '测试产地',
                        stock: 50,
                        description: '这是一个测试商品'
                    })
                });
                
                const data = await response.json();
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 商品添加成功: ${data.data.name}</div>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 添加商品失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 添加商品失败: ${error.message}</div>`;
            }
        }

        async function testGetProducts() {
            const result = document.getElementById('productsResult');
            if (!currentSeller) {
                result.innerHTML = '<div class="error">请先登录</div>';
                return;
            }
            
            try {
                const response = await fetch(`http://localhost:8000/api/seller/products?username=${currentSeller.username}`);
                const data = await response.json();
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 商品列表: 共${data.data.total}个商品</div>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 获取商品列表失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 获取商品列表失败: ${error.message}</div>`;
            }
        }

        async function testUpdateShop() {
            const result = document.getElementById('updateShopResult');
            if (!currentSeller) {
                result.innerHTML = '<div class="error">请先登录</div>';
                return;
            }
            
            try {
                const response = await fetch('http://localhost:8000/api/seller/shop', {
                    method: 'PUT',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        username: currentSeller.username,
                        name: '更新后的店铺名称',
                        description: '这是更新后的店铺描述',
                        address: '更新后的店铺地址'
                    })
                });
                
                const data = await response.json();
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 店铺信息更新成功: ${data.data.name}</div>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 更新店铺信息失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 更新店铺信息失败: ${error.message}</div>`;
            }
        }

        async function testGetStats() {
            const result = document.getElementById('statsResult');
            if (!currentSeller) {
                result.innerHTML = '<div class="error">请先登录</div>';
                return;
            }
            
            try {
                const response = await fetch(`http://localhost:8000/api/seller/stats?username=${currentSeller.username}`);
                const data = await response.json();
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 统计数据: 商品${data.data.total_products}个, 销售额¥${data.data.total_revenue}</div>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 获取统计数据失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 获取统计数据失败: ${error.message}</div>`;
            }
        }
    </script>
</body>
</html>
